Imports System.Drawing
Imports System.Runtime.InteropServices

Friend Class ExtractIcon
  Friend Class UnsafeNativeMethods

    <DllImport("shell32", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Friend Shared Function ExtractIcon( _
      ByVal hInst As IntPtr, _
      ByVal lpszExeFileName As String, _
      ByVal nIconIndex As Integer) As IntPtr
    End Function

    <DllImport("shell32", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Friend Shared Function PickIconDlg( _
      ByVal hwndOwner As IntPtr, _
      ByVal lpstrFile As System.Text.StringBuilder, _
      ByVal nMaxFile As Integer, _
      ByRef lpdwIconIndex As Integer) As Integer
    End Function

    Private Sub New()

    End Sub

  End Class

  <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")> _
  Shared Function GetIcon(ByVal iconFile As String, ByVal iconIndex As Integer) As System.Drawing.Icon

    Dim hIcon As IntPtr = UnsafeNativeMethods.ExtractIcon(Process.GetCurrentProcess.Handle, iconFile, iconIndex)
    Return Icon.FromHandle(hIcon)

  End Function

  Private Sub New()

  End Sub

End Class
